package com.example.backend.controller;

import com.example.backend.entity.StockRecord;
import com.example.backend.service.StockRecordService;
import com.example.backend.util.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/stock-record")
public class StockRecordController {
    @Autowired
    private StockRecordService stockRecordService;

    // 查询所有库存记录
    @GetMapping
    public ApiResponse<List<StockRecord>> getAllStockRecords() {
        return ApiResponse.success(stockRecordService.getAllStockRecords());
    }

    // 按港口筛选
    @GetMapping("/port/{portId}")
    public ApiResponse<List<StockRecord>> getStockRecordsByPort(@PathVariable("portId") Long portId) {
        List<StockRecord> all = stockRecordService.getAllStockRecords();
        List<StockRecord> filtered = all.stream().filter(r -> r.getPortId().equals(portId)).toList();
        return ApiResponse.success(filtered);
    }

    // 按仓库筛选
    @GetMapping("/warehouse/{warehouseId}")
    public ApiResponse<List<StockRecord>> getStockRecordsByWarehouse(@PathVariable("warehouseId") Long warehouseId) {
        return ApiResponse.success(stockRecordService.getStockRecordsByWarehouseId(warehouseId));
    }

    // 按港口+仓库联合筛选
    @GetMapping("/port/{portId}/warehouse/{warehouseId}")
    public ApiResponse<List<StockRecord>> getStockRecordsByPortAndWarehouse(@PathVariable("portId") Long portId, @PathVariable("warehouseId") Long warehouseId) {
        List<StockRecord> all = stockRecordService.getStockRecordsByWarehouseId(warehouseId);
        List<StockRecord> filtered = all.stream().filter(r -> r.getPortId().equals(portId)).toList();
        return ApiResponse.success(filtered);
    }
}